<template>
  <div class="app-container">
    <el-form :model="queryParams" :rules="rules" :visible.sync="open" ref="form"  size="small" :inline="true" v-show="showSearch" label-width="68px">
      <p>基础信息</p>
      <el-form-item label="出库类型" prop="outboundType">
      		  <el-select v-model="queryParams.outboundType" placeholder="请选择出库类型" clearable size="mini">
      			<el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
      					   :value="dict.dictValue"/>
      		  </el-select>
      </el-form-item>
      <el-form-item label="关联工单" prop="">
        <el-select v-model="queryParams.guanliangongdan" placeholder="请选择关联工单" clearable size="mini">
        <el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
               :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="关联项目" prop="projectNo">
        <el-select v-model="queryParams.projectNo" placeholder="请选择关联项目" clearable size="mini">
        <el-option v-for="dict in binding_status " :key="dict.dictValue" :label="dict.dictLabel"
               :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="出库主题" prop="outboundTheme">
        <el-input
          v-model="queryParams.outboundTheme"
          clearable
          @keyup.enter.native="outboundTheme"
        />
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker v-model="daterangeOutboundTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="领料人员" prop="pickingPerson">
        <el-input
          v-model="queryParams.pickingPerson"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库人员" prop="outboundPerson">
        <el-input
          v-model="queryParams.outboundPerson"
          placeholder="默认当前输入的人员，可自定义输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
		<el-form-item label="出库仓库">
		  <el-select v-model="queryParams.warehouseName" placeholder="仓库名称" clearable size="mini">
			<el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
					   :value="dict.dictValue"/>
		  </el-select>
		  <el-select v-model="queryParams.warehouseArea" placeholder="仓库区域" clearable size="mini">
			<el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
					   :value="dict.dictValue"/>
		  </el-select>
		</el-form-item>
    </el-form>
    <p>物料明细</p>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:outboundOrder:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material:outboundOrder:remove']"
        >移除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outboundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="物料类别" align="center" prop="materialType" />
	  <el-table-column label="物品名称" align="center" prop="" />
      <el-table-column label="出库单号" align="center" prop="outboundNo" />
	  <el-table-column label="规格" align="center" prop="specifications" />
	  <el-table-column label="单位" align="center" prop="unit" />
	  <el-table-column label="分配数量" align="center" prop="count" />
	  <el-table-column label="剩余分配数量" align="center" prop="" />
	  <el-table-column label="本次出库数量" align="center" prop="">
		<template slot-scope="scope" >
              <el-input-number v-model="scope.row.count" @change="inputChangeNumber(scope.row)" controls-position="right"
                               :min="0" :max="scope.row.remainderCount" label="描述文字">
              </el-input-number>
            </template>
	  </el-table-column>
    </el-table>
     <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="quxiao">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>


      <!-- 对话框 -->
      <el-dialog :title="title" :visible.sync="open"  append-to-body>
        <el-form ref="form" :model="form" label-width="80px">
                <!-- 已选 几项 -->
                <!-- <div style="font-size: 20px">
                  已勾选<span>{{ valLength }}</span>项
                </div> -->
                <el-table v-loading="loading" :data="WarehouseInfoList" @selection-change="handleSelectionChange1">

                  <el-table-column type="selection" width="55" align="center"/>
                  <el-table-column label="序号" width="55" align="center" prop="id"/>
                  <el-table-column label="工单编号" width="100" align="center" prop="materialName"/>
                  <el-table-column label="工单主题" width="100" align="center" prop="materialType"/>
                  <el-table-column label="关联计划" width="100" align="center" prop="specifications"/>
                  <el-table-column label="关联项目名称" width="100" align="center" prop="remainderCount"/>
                  <el-table-column label="创建时间" width="100" align="center" prop="nums" />
                  <el-table-column label="构件概要" width="100" align="center" prop="nums"/>
                  <el-table-column label="零件概要" width="100" align="center" prop="nums"/>
                    <!-- <template slot-scope="scope" >
                        <el-input-number v-model="scope.row.count" @change="inputChangeNumber(scope.row)" controls-position="right"
                                         :min="0" :max="scope.row.remainderCount" label="描述文字">
                        </el-input-number>
                    </template> -->

                </el-table>
              </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOutboundOrder, getOutboundOrder, delOutboundOrder, addOutboundOrder, updateOutboundOrder, wuliao } from "@/api/material/outboundOrder";
export default {
  name: "OutboundOrder",
  dicts: ["binding_status", "partnership_level"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMaterialInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库表格数据
      outboundOrderList: [],
      // 物料信息表格数据
      materialInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeOutboundTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outboundNo: null,
        orderNo: null,
        projectNo: null,
        warehouseName: null,
        warehouseArea: null,
        outboundPerson: null,
        outboundTime: null,
        pickingPerson: null,
        outboundTheme:null,
        count:null,
        remainderCount:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outboundTheme:[
       { required: true, message: "出库主题不能为空", trigger: "change"}
        ],
      //   outboundTime:[
      //  { required: true, message: "出库时间不能为空", trigger: "change"}
      //   ],
        outboundNo:[
       { required: true, message: "出库单号不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    queding() {
          //   updateInfo(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
            
          // }
          this.$refs['form'].validate(valid=>{
            if(valid){
              if(this.form.id != null){
                  console.log(123)
              }else{
                wuliao(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$router.go(-1)
              
              this.getList();
            });
              }
            }
          })
          
    },
    quxiao(){
      this.$router.go(-1)
    },
    /** 查询出库列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOutboundTime && '' != this.daterangeOutboundTime) {
        this.queryParams.params["beginOutboundTime"] = this.daterangeOutboundTime[0];
        this.queryParams.params["endOutboundTime"] = this.daterangeOutboundTime[1];
      }
      listOutboundOrder(this.queryParams).then(response => {
        this.outboundOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outboundNo: null,
        outboundType: null,
        orderNo: null,
        projectNo: null,
        guanliangongdan:null,
        warehouseName: null,
        warehouseArea: null,
        outboundTheme: null,
        outboundPerson: null,
        outboundTime: null,
        pickingPerson: null,
        createTime: null,
        createBy: null,
        status: "0",
        comment: null,
        remainderCount: null,
        count:null,
      };
      this.materialInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOutboundTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	this.$router.push('/MaterialInfo/outboundOrderX')
      this.reset();
      const id = row.id || this.ids
      getOutboundOrder(id).then(response => {
        this.form = response.data;
        this.materialInfoList = response.data.materialInfoList;
        this.open = true;
        this.title = "修改出库";
      });
    },
	handleLook(row){
		this.$router.push('/MaterialInfo/outboundOrderLook')
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.materialInfoList = this.materialInfoList;
          if (this.form.id != null) {
            updateOutboundOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$router.go(-1)
            });
          } else {
            addOutboundOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            this.$router.go(-1)
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库编号为"' + ids + '"的数据项？').then(function() {
        return delOutboundOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 物料信息序号 */
    rowMaterialInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 物料信息添加按钮操作 */
    handleAddMaterialInfo() {
      let obj = {};
      obj.projectNo = "";
      obj.materialNo = "";
      obj.materialName = "";
      obj.materialType = "";
      obj.specifications = "";
      obj.unit = "";
      obj.count = "";
      obj.comment = "";
      this.materialInfoList.push(obj);
    },
    /** 物料信息删除按钮操作 */
    handleDeleteMaterialInfo() {
      if (this.checkedMaterialInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的物料信息数据");
      } else {
        const materialInfoList = this.materialInfoList;
        const checkedMaterialInfo = this.checkedMaterialInfo;
        this.materialInfoList = materialInfoList.filter(function(item) {
          return checkedMaterialInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMaterialInfoSelectionChange(selection) {
      this.checkedMaterialInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material/outboundOrder/export', {
        ...this.queryParams
      }, `outboundOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
  .dialog-footer {
    text-align: center;
    margin-top: 15px;

  }
</style>
